﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Web.UI.WebControls;

namespace Thksoft.Modal
{
    public partial class EditBillsLedger_Bu : BasePage
    {
        private YX_BILLSLEDGER_BU_BLL bubll = new YX_BILLSLEDGER_BU_BLL();
        private Sys_UserBLL user = new Sys_UserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDateStart.Value = DateTime.Now.ToString("yyyy-MM-dd");

                BindBank();
                BindACCOUNT();
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    BindData();
                }
            }
        }

        /// <summary>
        /// 修改绑定数据
        /// </summary>
        public void BindData()
        {
            var id = Convert.ToInt32(Request.QueryString["ID"].ToString());

            var model = bubll.SelectById(id);
            if (model != null)
            {
                FORM_Type.Text = model.N_TYPE.ToString();
                txtBillnum.Text = model.C_BILLCODE.ToString();
                //txtDRAWERBank.Value = model.C_DRAWERBANKNAME.ToString();
                FORM_BANK_ID.SelectedItem.Text = model.C_DRAWERBANKNAME.ToString();
                txtDRAWERCompany.Value = model.N_DRAWERCMYID;
                txtZnum.SelectedItem.Text = model.C_DRAWERACCOUNT.ToString();
                //txtDrawer.Value = model.C_DRAWERUSERNAME.ToString();
                txtDRAWEENAME.Value = model.C_DRAWEENAME.ToString();

                txtPayeeBank.Value = model.C_PAYEEBANKNAME.ToString();
                txtPayeeCompany.Value = model.C_PAYEECMYNAME.ToString();
                txtDfzh.Value = model.C_PAYEEACCOUNT.ToString();
                //txtPayee.Value = model.C_PAYEEUSERNAME.ToString();
                txtPayeeNumber.Value = model.C_PAYEENUMBER.ToString();

                txtamont1.Value = System.Decimal.Round(Convert.ToDecimal(model.N_INVOICEVALUE), 4).ToString();
                txtDateStart.Value = model.D_STARTDATE == null ? "" : model.D_STARTDATE.ToString("yyyy-MM-dd");
                txtDateEnd.Value = model.D_ENDDATE == null ? "" : model.D_ENDDATE.ToString("yyyy-MM-dd");
                txtTERMOFBILL.Value = model.N_TERMOFBILL.ToString();
                txtACCEPTANCETIME.Value = model.D_ACCEPTANCETIME == null ? "" : model.D_ACCEPTANCETIME.ToString("yyyy-MM-dd");
                txtCOLLECTTICKETSTIME.Value = model.D_COLLECTTICKETSTIME == null ? "" : model.D_COLLECTTICKETSTIME.ToString("yyyy-MM-dd");
                txtPROJECTCODE.Text = model.C_PROJECTCODE.ToString();
                txtPROJECTNAME.Text = model.C_PROJECTNAME.ToString();

                ddlCheckType.SelectedValue = model.N_IFOFFER.ToString();
                ddlPayType.SelectedValue = model.N_PAYTYPE.ToString();
                ddlState.SelectedValue = model.N_STATE.ToString();
                txtSUM.Text = model.N_SUM.ToString();
                txtRemark.Text = model.C_REMARK.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 编辑条件

            if (FORM_Type.Text == "-1")
            {
                MessageBox.Show(Page, "请选择票据种类");
                return;
            }
            if (txtBillnum.Text.Trim() == "")
            {
                MessageBox.Show(Page, "票据编号不能为空");
                return;
            }
            if (txtDRAWERCompany.Value == -1)
            {
                MessageBox.Show(Page, "出票人公司名称（出票人）不能为空");
                return;
            }
            if (FORM_BANK_ID.Text == "-1")
            {
                MessageBox.Show(Page, "出票人银行名称不能为空");
                return;
            }
            if (txtZnum.Text == "-1")
            {
                MessageBox.Show(Page, "出票人账户不能为空");
                return;
            }

            if (txtPayeeCompany.Value.Trim() == "")
            {
                MessageBox.Show(Page, "收款人公司名称（收款人）不能为空");
                return;
            }

            if (txtamont1.Value.Trim() == "")
            {
                MessageBox.Show(Page, "出票金额不能为空");
                return;
            }
            else
            {
                decimal je = 0;
                if (!decimal.TryParse(txtamont1.Value.Trim(), out je))
                {
                    MessageBox.Show(Page, "出票金额必须是数字");
                    return;
                }
                else
                {
                    if (Convert.ToDecimal(txtamont1.Value.Trim()) < 1000)
                    {
                        MessageBox.Show(Page, "票据金额最低为1000");
                        return;
                    }
                }
            }

            if (txtDateStart.Value.Trim() == "")
            {
                MessageBox.Show(Page, "出票日期不能为空");
                return;
            }

            #endregion 编辑条件

            var model = new YX_BILLSLEDGER_BU();

            model.N_TYPE = Convert.ToDecimal(FORM_Type.SelectedValue);
            model.C_BILLCODE = txtBillnum.Text.Trim();
            model.C_DRAWERCMYNAME = txtDRAWERCompany.Text;
            //model.C_DRAWERBANKNAME = txtDRAWERBank.Value.Trim();
            model.C_DRAWERBANKNAME = FORM_BANK_ID.SelectedItem.Text;
            model.C_DRAWERACCOUNT = txtZnum.SelectedItem.Text;
            //model.C_DRAWERUSERNAME = txtDrawer.Value.Trim();
            model.N_DRAWERCMYID = txtDRAWERCompany.Value;
            model.C_DRAWEENAME = txtDRAWEENAME.Value.Trim();
            model.C_PAYEEBANKNAME = txtPayeeBank.Value.Trim();
            model.C_PAYEECMYNAME = txtPayeeCompany.Value.Trim();
            model.C_PAYEEACCOUNT = txtDfzh.Value.Trim();
            //model.C_PAYEEUSERNAME = txtPayee.Value.Trim();
            model.N_PAYEECMYID = Convert.ToDecimal(FORM_CUSTOMER_ID.Value.Trim() == "" ? null : FORM_CUSTOMER_ID.Value);
            model.C_PAYEENUMBER = txtPayeeNumber.Value.Trim();
            model.N_INVOICEVALUE = System.Decimal.Round(Convert.ToDecimal(txtamont1.Value), 4);
            model.C_PROJECTCODE = txtPROJECTCODE.Text;
            model.C_PROJECTNAME = txtPROJECTNAME.Text.Trim();
            model.D_STARTDATE = Convert.ToDateTime(txtDateStart.Value == "" ? null : txtDateStart.Value);
            model.D_ENDDATE = Convert.ToDateTime(txtDateEnd.Value == "" ? null : txtDateEnd.Value);//DateTime.Parse(txtDateEnd.Value);"yyyy-mm-dd hh24:MI:ss"
            model.N_TERMOFBILL = Convert.ToDecimal(txtTERMOFBILL.Value.Trim() == "" ? null : txtTERMOFBILL.Value.Trim());
            model.D_ACCEPTANCETIME = Convert.ToDateTime(txtACCEPTANCETIME.Value == "" ? null : txtACCEPTANCETIME.Value);
            model.D_COLLECTTICKETSTIME = Convert.ToDateTime(txtCOLLECTTICKETSTIME.Value == "" ? null : txtCOLLECTTICKETSTIME.Value);
            model.N_IFOFFER = decimal.Parse(ddlCheckType.SelectedValue);
            model.N_PAYTYPE = decimal.Parse(ddlPayType.SelectedValue);
            model.N_SUM = Convert.ToDecimal(txtSUM.Text == "" ? "1" : txtSUM.Text);
            model.N_STATE = Convert.ToDecimal(ddlState.SelectedValue);
            model.C_REMARK = txtRemark.Text.ToString();
            model.N_ENTRYSTAFF = CurrentUserID;

            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                try
                {
                    model.N_ID = Convert.ToDecimal(Request.QueryString["ID"]);
                    if (bubll.UPDATE(model, CurrentUserName))
                    {
                        MessageBox.Show(Page, "编辑成功！");
                    }
                    else
                    {
                        MessageBox.Show(Page, "编辑失败！");
                        return;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show(Page, "编辑失败！");
                }
            }
            else
            {
                model.N_ID = 1;
                if (bubll.Insert(model, CurrentUserName))
                {
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Modal/EditBillsLedger_Bu.aspx");
                }
                else
                {
                    MessageBox.Show(Page, "新增失败！");
                    return;
                }
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_CLOSE();");
        }

        #region 下拉列表设置

        /// <summary>
        /// 得到汇票银行
        /// </summary>
        public void BindBank()
        {
            int count;

            var dt = new Sys_BankBLL().GetEntitys(null, null, 0, 1000, out count);

            FORM_BANK_ID.DataSource = dt;
            FORM_BANK_ID.DataTextField = "C_NAME";
            FORM_BANK_ID.DataValueField = "N_ID";
            FORM_BANK_ID.DataBind();
            FORM_BANK_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));

            FORM_BANK_ID.SelectedValue = "-1";
        }

        /// <summary>
        /// 得到汇票账户
        /// </summary>
        public void BindACCOUNT(decimal bankId = 0)
        {
            int count;

            var dt = new Yx_AccountBLL().GetEntitys(null, null, 0, 1000, out count, bankId);

            txtZnum.DataSource = dt;
            txtZnum.DataTextField = "Account_No";
            txtZnum.DataValueField = "Account_Id";
            txtZnum.DataBind();
            txtZnum.Items.Insert(0, new ListItem("---请选择---", "-1"));

            txtZnum.SelectedValue = "-1";
        }

        /// <summary>
        /// 所属种类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void FORM_BANK_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindACCOUNT(Convert.ToDecimal(FORM_BANK_ID.SelectedValue));
        }

        #endregion 下拉列表设置
    }
}